package com.demo.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.demo.interceptor.MyInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * 配置包扫描路径，扫描的注解类型，以及 MVC
 */
@Configuration
@ComponentScan(basePackages = "com.demo", includeFilters =
        {@ComponentScan.Filter(classes = {Configuration.class, RestController.class, Service.class})})
public class SpringConfig extends WebMvcConfigurationSupport {

    /**
     * 静态资源配置
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /**
     * 拦截器配置
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
    }

    /**
     * 消息转换器
     * @param converters
     */
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new FastJsonHttpMessageConverter());
    }

}
